<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>原生JS实现对象深拷贝</title>
  </head>
  <body></body>

  <script>
    var temp = {
      name: "测试",
      age: 18,
      test: {
        list: [1, 2],
      },
    };
    temp.test.temp = temp;
    // const { port1, port2 } = new MessageChannel();
    // port2.onmessage = (message) => {
    //   const obj = message.data;
    //   obj.name = "111";
    //   console.log(obj, temp);
    // };
    // port1.postMessage(temp);
    console.log(temp);

    function deepClone(obj) {
      return new Promise((resolve, reject) => {
        const { port1, port2 } = new MessageChannel();
        port2.onmessage = (message) => {
          const temp = message.data;
          resolve(temp);
        };
        port1.postMessage(obj);
      });
    }
    // deepClone(temp).then((res) => {
    //   console.log(res);
    // });

    class Singleton {
      constructor(name) {
        this.name = name;
      }
      // 静态方法
      static getInstance(name) {
        if (!this.instance) {
          this.instance = new Singleton(name);
        }
        return this.instance;
      }
    }
    let a = Singleton.getInstance("a1");
    let b = Singleton.getInstance("b2");
    console.log(a === b); // true
  </script>
</html>
